package com.hc.server.conn.mall;

import java.io.IOException;
import java.net.MalformedURLException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import com.hc.server.Config;
import com.hc.server.util.HttpConnect;
import com.hc.server.util.Util;

public class MallServerHelper {
	
	private static final String QUERY_TICKETS_URI = "/api/pdt/qscenics.do";
	private static final String QUERY_TICKET_URI = "/api/pdt/qscenic.do";
	private static final String QUERY_HOTELS_URI = "/api/pdt/qhotels.do";
	private static final String QUERY_HOTEL_URI = "/api/pdt/qhotel.do";
	private static final String BOOK_GOODS_URI = "/api/pdt/aorder.do";
	private static final String MODIFY_ORDER_STATUS_URI = "/api/pdt/uorder.do";
	private static final String QUERY_ACTS_URI = "/api/pdt/qactivitys.do";
	private static final String QUERY_ACT_URI = "/api/pdt/qactivity.do";
	private static final String QUERY_AIRLINES_URI = null;
	private static final String QUERY_AIRLINE_URI = null;
	private static final String QUERY_TRAVELS_URI = null;
	private static final String QUERY_TRAVEL_URI = null;
	private static final String GOODS_STATUS_URI = "/api/pdt/qstock.do";
	
	private static final String CITY = "canton";
	private static final String BOOKDATE = "bookDate";
	private static final String CHECKIN = "sDate";
	private static final String CHECKOUT = "eDate";
	private static final String LV = "level";
	private static final String NAME = "name";
	private static final String KEY = "keyWord";
	private static final String PAGE_INDEX = "currentPage";
	private static final String PAGE_SIZE = "rowsOfPage";
	private static final String PDT_SN = "pdtSn";
    private static final String PDT_TYPE = "pdtType";
	private static final String PDT_NAME = "pdtName";
	private static final String BOOK_COUNT = "count";
	private static final String CHECKIN_DATE = "sdate";
	private static final String CHECKOUT_DATE = "edate";
	private static final String REMARK = "remark";
	private static final String CP_NAME = "certify_name";
	private static final String CP_PHONE = "certify_phone";
	private static final String CP_CER_TYPE = "certify_type";
	private static final String CP_CER_NUM = "certify_sn";
	private static final String CP_ADDRESS = "certify_address";
	private static final String ORDER_SN = "orderSn";
	private static final String ORDER_OP = "operate";
	private static final String SORT = "sort";

	
	/**
	 * 
	 * @param city
	 * @param date
	 * @param name
	 * @param key
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 * @throws IOException 
	 * @throws JSONException
	 *             只有系统没有调试好才会出现该问题
	 */
	public static MallResponse queryTickets(String city, String date,
			String name, String key, int orderBy, int pageIndex, int pageSize)
			throws JSONException, IOException {
		// 构建参数
		MallRequest req = new MallRequest();
		req.put(CITY, city);
		if (null != date && !date.isEmpty()) {
			req.put(BOOKDATE, date);
		}
		if (null != name && !name.isEmpty()) {
			req.put(NAME, name);
		}
		if (null != key && !key.isEmpty()) {
			req.put(KEY, key);
		}
		if(0 != orderBy && orderBy < 4){
			req.put(SORT, orderBy);
		}else{
			req.put(SORT, Config.SORT_ASC);
		}
		req.put(PAGE_INDEX, pageIndex);
		req.put(PAGE_SIZE, pageSize);

		MallResponse resp = postData(QUERY_TICKETS_URI, req);
		return resp;
	}

	public static MallResponse queryTicket(String pdtSn, String checkInDate) throws JSONException, IOException {
		MallRequest req = new MallRequest();
		req.put(PDT_SN, pdtSn);
		if( null != checkInDate && !checkInDate.isEmpty()){
			req.put(BOOKDATE, checkInDate);
		}
		MallResponse resp = postData(QUERY_TICKET_URI, req);
		return resp;
	}

	public static MallResponse queryTravels(String city, String date,
			String name, String key, int orderBy, int pageIndex, int pageSize)
			throws JSONException, IOException {
		// 构建参数
		MallRequest req = new MallRequest();
		req.put(CITY, city);
		if (null != date && !date.isEmpty()) {
			req.put(BOOKDATE, date);
		}
		if (null != name && !name.isEmpty()) {
			req.put(NAME, name);
		}
		if (null != key && !key.isEmpty()) {
			req.put(KEY, key);
		}
		if(0 != orderBy && orderBy < 4){
			req.put(SORT, orderBy);
		}else{
			req.put(SORT, Config.SORT_ASC);
		}
		req.put(PAGE_INDEX, pageIndex);
		req.put(PAGE_SIZE, pageSize);

		MallResponse resp = postData(QUERY_TICKETS_URI, req);
		return resp;
	}
	
	public static MallResponse queryTravel(String pdtSn, String checkInDate) throws IOException, JSONException {
		MallRequest req = new MallRequest();
		req.put(PDT_SN, pdtSn);
		if( null != checkInDate && !checkInDate.isEmpty()){
			req.put(BOOKDATE, checkInDate);
		}
		MallResponse resp = postData(QUERY_TRAVEL_URI, req);
		return resp;
	}
	
	public static MallResponse queryHotels(String city, String inDate, String outDate,
			String name, int lv, String key, int orderBy, int pageIndex, int pageSize)
			throws JSONException, IOException {
		// 构建参数
		MallRequest req = new MallRequest();
		req.put(CITY, city);
		req.put(CHECKIN, inDate);
		req.put(CHECKOUT, outDate);
		if (null != name && !name.isEmpty()) {
			req.put(NAME, name);
		}
		if (null != key && !key.isEmpty()) {
			req.put(KEY, key);
		}
		if(0 != lv){
			req.put(LV, lv);
		}
		if(0 != orderBy && orderBy < 4){
			req.put(SORT, orderBy);
		}else{
			req.put(SORT, Config.SORT_ASC);
		}
		req.put(PAGE_INDEX, pageIndex);
		req.put(PAGE_SIZE, pageSize);
									 
		MallResponse resp = postData(QUERY_HOTELS_URI, req);
		return resp;
	}
	
	public static MallResponse queryHotel(int hotelID, String inDate, String outDate) throws JSONException, IOException {
		MallRequest req = new MallRequest();
		req.put("id", hotelID);
		req.put(CHECKIN, inDate);
		req.put(CHECKOUT, outDate);
		
		MallResponse resp = postData(QUERY_HOTEL_URI, req);
		return resp;
	}
	
	public static MallResponse bookGoods(JSONObject goodsInfo, JSONObject contactInfo) throws IllegalArgumentException, IOException, JSONException{
		MallRequest req = new MallRequest();
		try {
			String sn = goodsInfo.getString("SN");
			req.put(PDT_SN, sn);
			int type = goodsInfo.getInt("TYPE");
			req.put(PDT_TYPE, type);
			String name = goodsInfo.getString("NAME");
			req.put(PDT_NAME, name);
			int count = goodsInfo.getInt("COUNT");
			req.put(BOOK_COUNT, count);
			//Date checkIn = Util.Date2SQLDate();
			String checkIn = goodsInfo.getString("CHECKIN_DATE");
			req.put(CHECKIN_DATE, checkIn);
			if(Config.TYPE_HOTEL == type){
				//Date checkOut = Util.Date2SQLDate(goodsInfo.getString("CHECKOUT_DATE"));
				String checkOut = goodsInfo.getString("CHECKOUT_DATE");
				req.put(CHECKOUT_DATE, checkOut);
			}
			String des = goodsInfo.optString("DES");
			req.put(REMARK, des);//备注的定义?
			String cpName = contactInfo.getString("NAME");
			req.put(CP_NAME, cpName);
			String cpPhone = contactInfo.getString("PHONE");
			req.put(CP_PHONE, cpPhone);
			switch (type) {
				case Config.TYPE_HOTEL:
				case Config.TYPE_ACT:
				case Config.TYPE_TICKET:
				case Config.TYPE_TRAVEL:
				case Config.TYPE_AIR://FIXME:机票不对
					int idType = contactInfo.getInt("ID_TYPE");
					req.put(CP_CER_TYPE, idType);
					String idNum = contactInfo.getString("ID_NUM");
					req.put(CP_CER_NUM, idNum);
					break;
				case Config.TYPE_SPECIAL:
					String address = contactInfo.getString("ADD");
					req.put(CP_ADDRESS, address);
					break;
				default:
					throw new IllegalArgumentException();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new IllegalArgumentException();
		}
		MallResponse resp = postData(BOOK_GOODS_URI, req);
		
		return resp;
	}
	
	private static final int OP_PAY = 10;
	private static final int OP_CANCEL = 20;
	
	public static MallResponse payOrder(String mallOrderId) throws IOException, JSONException {
		MallRequest req = new MallRequest();
		req.put(ORDER_SN, mallOrderId);
		req.put(ORDER_OP, OP_PAY);
		MallResponse resp = postData(MODIFY_ORDER_STATUS_URI, req);
		return resp;
	}
	
	public static MallResponse cancelOrder(String mallOrderId) throws IOException, JSONException {
		MallRequest req = new MallRequest();
		req.put(ORDER_SN, mallOrderId);
		req.put(ORDER_OP, OP_CANCEL);
		MallResponse resp = postData(MODIFY_ORDER_STATUS_URI, req);
		return resp;
	}

	public static MallResponse queryActivities(String city, String name, int pageIndex, int pageSize) throws IOException, JSONException{
		MallRequest req = new MallRequest();
		req.put(CITY, city);
		if(null != name && !name.isEmpty()){
			req.put(NAME, name);
		}
		req.put(SORT, Config.SORT_ASC);
		req.put(PAGE_INDEX, pageIndex);
		req.put(PAGE_SIZE, pageSize);
		MallResponse resp = postData(QUERY_ACTS_URI, req);
		return resp;
	}
	
	public static MallResponse queryActivity(String pdtSn) throws IOException, JSONException{
		MallRequest req = new MallRequest();
		req.put(PDT_SN, pdtSn);
		MallResponse resp = postData(QUERY_ACT_URI, req);
		return resp;
	}
	

	private static final String FROM_CITY = null;
	private static final String TO_CITY = null;
	
	public static MallResponse queryAirlines(String fromCity, String toCity, String checkInDate, int orderBy) throws IOException, JSONException {
		MallRequest req = new MallRequest();
		req.put(FROM_CITY, fromCity);
		req.put(TO_CITY, toCity);
		req.put(BOOKDATE, checkInDate);
		if(0 != orderBy && 3 != orderBy  && orderBy < 5){
			req.put(SORT, orderBy);
		}else{
			req.put(SORT, Config.SORT_ASC);
		}
		MallResponse resp = postData(QUERY_AIRLINES_URI, req);
		return resp;
	}
	
	public static MallResponse queryAirline(String pdtSn, String checkInDate) throws IOException, JSONException {
		MallRequest req = new MallRequest();
		req.put(PDT_SN, pdtSn);
		req.put(BOOKDATE, checkInDate);
		MallResponse resp = postData(QUERY_AIRLINE_URI, req);
		return resp;
	}
	
	public static MallResponse getStatus(String pdtSn) throws IOException, JSONException {
		MallRequest req = new MallRequest();
		req.put(PDT_SN, pdtSn);
		Date now = new Date();
		try {
			String today = Util.Date2Calender(now);
			req.put("bDate", today);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		req.put("type", 2);//1.查询当日，2.查询30条数据
		MallResponse resp = postData(GOODS_STATUS_URI, req);
		return resp;
	}
	
	private static MallResponse postData(String uri, MallRequest req)
			throws IOException, JSONException {
		HttpConnect conn = null;
		MallResponse resp = null;
		try {
			conn = new HttpConnect(Config.MALL_SERVER_URL + uri, "application/x-www-form-urlencoded");
			System.out.println(req.toString());
			conn.SendData(req.toString());
			String respMsg = conn.ReceiveUTFString();
			System.out.println(respMsg);
			resp = MallResponse.paser(respMsg);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.releaseHttpConnect();
			}
		}
		return resp;
	}
}
